package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.model.IsmAppSetting;
import com.sec.etech.ism.model.IsmServiceDef;
import com.sec.etech.ism.service.IsmAppSettingService;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmServiceDefService;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.form.util.DateUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class IsmSendPlanWarehouseDateServiceImpl implements IsmMethodService {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    IsmCommonService ismCommonService;
    @Resource
    IsmAppSettingService ismAppSettingService;
    @Resource
    IsmServiceDefService ismServiceDefService;

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject busObj) throws Exception {
        JSONObject result = new JSONObject();
        String msg = this.sendPlanWarehouseDate(sysCode, serviceCode);
        result.put("flag", msg);
        result.put("msg", msg);
        return result;
    }

    private String sendPlanWarehouseDate(String sysCode, String serviceCode) {
        Map<String, String> logMap = new HashMap<>(5);
        //服务信息
        IsmServiceDef serviceDef = null;
        try {
            serviceDef = ismServiceDefService.getByCode(serviceCode);
            //调用的应用信息
            IsmAppSetting appSetting = ismAppSettingService.getByCode(sysCode);
            String url = appSetting.getAppAddress() + "prodInfo/upDatePlanWarehouseDate";
            logMap.put("appName", this.methodName());
            logMap.put("url", url);

            String sql = "SELECT a.plantasknum,a.planwarehousedate FROM proj_plantask AS a,prod_component AS b,proj_project_order AS c " +
                    "WHERE a.plantaskid=b.componentid AND a.active=1 AND b.active=1 AND a.projectid=c.projectid AND c.projectid>0 " +
                    " AND c.turningdate>NOW() AND a.planwarehousedate IS NOT NULL";
            List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
            if (list == null || list.size() == 0) {
                return "success";
            }
            List<JSONObject> pcsParams = new ArrayList<>(list.size());
            for (Map<String, Object> map : list) {
                JSONObject object = new JSONObject();
                object.put("componentNum", map.get("plantasknum"));
                if (map.get("planwarehousedate") != null) {
                    object.put("planWarehouseDate", DateUtils.format((Date) map.get("planwarehousedate"), "yyyy-MM-dd"));
                } else {
                    object.put("planWarehouseDate", "");
                }
                pcsParams.add(object);
            }
            ismCommonService.checkResult(url, JSONObject.toJSONString(pcsParams), serviceDef, null, null, this.methodName(), logMap);
            return "success";
        } catch (Exception e) {
            log.error("机组信息推送PCS接口异常。serviceDef.getCode()={}，serviceDef.getName()={}，logMap={}",
                    serviceDef.getCode(), serviceDef.getName(), logMap);
            e.printStackTrace();
            return "机组信息推送PCS接口异常，请联系管理员处理！";
        }
    }

    @Override
    public String methodName() {
        return "sendPlanWarehouseDate";
    }
}
